import { PusherService } from '@/core/pusher.service';
import { CurrentUser } from '@/decorators';
import {
  NoticeCreateDto,
  NoticeFindDto,
  NoticeListDto,
  NoticeQueryDto,
  NoticeRemoveDto,
  NoticeUpdateDto,
} from '@/dtos';
import { NoticeService } from '@/services/notice.service';
import { halt, ok } from '@/utils';
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
} from '@nestjs/common';
import { BaseController } from './base.controller';

@Controller('notice')
export class NoticeController extends BaseController {
  constructor(
    private readonly noticeService: NoticeService,
    private readonly pusherService: PusherService,
  ) {
    super();
  }

  @Post('list')
  async getList(@Body() dto: NoticeListDto, @CurrentUser('id') userId: number) {
    return ok(await this.noticeService.getList(userId, dto));
  }

  @Get('detail/:id')
  async getDetail(@Param('id') id: number) {
    return ok(
      await this.noticeService.findOne(
        {
          id,
          isDeleted: false,
          isEnabled: true,
        },
        {
          id: true,
          title: true,
          type: true,
          content: true,
          publishAt: true,
        },
      ),
    );
  }

  @Post('follow/:id')
  async follow(@Param('id') id: number, @CurrentUser('id') userId: number) {
    const data = await this.prisma.userFollowNotice.findFirst({
      where: {
        userId,
        noticeId: id,
      },
    });
    if (!!data) {
      await this.prisma.userFollowNotice.deleteMany({
        where: {
          userId,
          noticeId: id,
        },
      });
    } else {
      await this.prisma.userFollowNotice.create({
        data: {
          userId,
          noticeId: id,
        },
      });
    }
    return ok(true);
  }

  @Post('admin/push/:id')
  async push(@Param('id') id: number) {
    const notice = await this.prisma.notice.findUnique({ where: { id } });
    if (!notice || !notice.isEnabled) {
      return halt('公告信息异常或未发布');
    }
    try {
      await this.pusherService.sendMessage({
        content: notice.content,
        summary: notice.title,
        contentType: 2,
        topicIds: [31225],
        url: `${this.config.get('homeUrl')}/notice/detail/${id}`,
      });
    } catch (e) {
      console.log(e);
    }
    return ok(true);
  }

  @Post('admin/query')
  async query(@Body() dto: NoticeQueryDto) {
    return ok(await this.noticeService.query(dto));
  }

  @Post('admin/find')
  async find(@Body() dto: NoticeFindDto) {
    return ok(await this.noticeService.findOne(dto.where, dto.select));
  }

  @Post('admin/create')
  async create(@Body() dto: NoticeCreateDto) {
    return ok(await this.noticeService.create(dto.data));
  }

  @Patch('admin/update')
  async update(@Body() dto: NoticeUpdateDto) {
    return ok(await this.noticeService.update(dto));
  }

  @Delete('admin/remove')
  async remove(@Query() dto: NoticeRemoveDto) {
    return ok(await this.noticeService.remove(dto));
  }

  @Get('admin/popup/:id')
  async popup(@Param('id') id: number) {
    return ok(await this.noticeService.popup(id));
  }

  @Get('read/:id')
  async read(@Param('id') id: number, @CurrentUser('id') userId: number) {
    return ok(await this.noticeService.read(id, userId));
  }
}
